package serverAccess;

import java.util.ArrayList;
import java.util.Map;

import sparqlclient.SparqlClient;

public class ServerView {
	
	SparqlClient sparqlClient;
	
	public ServerView(){
		this.sparqlClient = new SparqlClient("localhost:3030/space");
	}
	/**
	 * Renvoie la liste des labels associé au label donne
	 * @param name le label donne
	 * @return
	 */
	/*public ArrayList<String> getLabel(String name){
		String query = "ASK WHERE { ?s ?p ?o }";
		System.out.println(sparqlClient.ask(query));
		
        query = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"+
        		"SELECT ?obj ?autrelab WHERE \n" 
        		+ "{ \n"
        		+ "?obj rdfs:label \"film\"@fr. \n"
        		+"?obj rdfs:label ?autrelab."
        		
        		+ "} \n";
        Iterable<Map<String, String>> results = sparqlClient.select(query);
        System.out.println("nombre de personnes par pièce: "+results.toString());
        for (Map<String, String> result : results) {
            System.out.println(result.get("obj") + " : " + result.get("autrelab"));
        }
        
		return null;
	}*/
	/**
	 * Attention sensible aux accents
	 * @param name le label sensible aux accents
	 * @return la liste des labels associés
	 */
	public ArrayList<String> getLabel(String name){
		String query = "ASK WHERE { ?s ?p ?o }";
		//System.out.println(sparqlClient.ask(query));
		ArrayList<String> toReturn = new ArrayList<String>();
		
        query = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
        		+ "SELECT ?obj ?autrelab WHERE \n" 
        		+ "{ \n"
        		+ "?obj rdfs:label \""+name+"\"@fr. \n"
        		+ "?obj rdfs:label ?autrelab."
        		+ "} \n";
        Iterable<Map<String, String>> results = sparqlClient.select(query);
        for (Map<String, String> result : results) {
            //System.out.println(/*result.get("obj") + " : " +*/ result.get("autrelab"));
        	toReturn.add(result.get("autrelab"));
        }
		return toReturn;
	}
	
	/**
	 * Renvoie la liste des sous-classes d'une entite
	 * @param name
	 * @return
	 */
	public ArrayList<String> getSubClass(String name){
		String query = "ASK WHERE { ?s ?p ?o }";
		ArrayList<String> toReturn = new ArrayList<String>();
		
        query = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
        		+ "SELECT ?class ?label WHERE \n" 
        		+ "{ \n"
        		+ "?obj rdfs:label \""+name+"\"@fr. \n"
        		+ "?class rdfs:subClassOf ?obj."
        		+ "?class rdfs:label ?label"
        		+ "} \n";
        Iterable<Map<String, String>> results = sparqlClient.select(query);
        //System.out.println("Liste des sous-classes: "+results.toString());
        for (Map<String, String> result : results) {
           // System.out.println(result.get("obj"));
        	toReturn.add(result.get("label"));
        }
        
		return toReturn;
	}
	
	/**
	 * Renvoie les instances d'une entite
	 * @param name
	 * @return
	 */
	public ArrayList<String> getInstance(String name){
		String query = "ASK WHERE { ?s ?p ?o }";
		ArrayList<String> toReturn = new ArrayList<String>();
		
        query = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
        		+ "PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"
        		+ "SELECT ?instance ?label WHERE \n" 
        		+ "{ \n"
        		+ "?obj rdfs:label \""+name+"\"@fr. \n"
        		+ "?instance rdf:type ?obj."
        		+ "?instance rdfs:label ?label"
        		+ "} \n";
        Iterable<Map<String, String>> results = sparqlClient.select(query);
        //System.out.println("Liste des instances: "+results.toString());
        for (Map<String, String> result : results) {
            //System.out.println(result.get("instance"));
        	toReturn.add(result.get("label"));
        }
        
		return toReturn;
	}
}
